package org.scohen.juploadr.uploadapi.flickrrest.geo;

import java.util.SortedMap;
import java.util.TreeMap;

import org.scohen.juploadr.app.UploadImage;
import org.scohen.juploadr.app.geo.GeoLocation;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.flickrrest.DefaultFlickrHandler;
import org.scohen.juploadr.uploadapi.flickrrest.FlickrMethod;
import org.scohen.juploadr.uploadapi.flickrrest.PhotoNotFoundException;
import org.scohen.juploadr.uploadapi.flickrrest.PostUploadAction;
import org.scohen.juploadr.uploadapi.flickrrest.RestFlickrApi;
import org.scohen.juploadr.uploadapi.flickrrest.transform.InvalidRotationException;
import org.scohen.juploadr.uploadapi.flickrrest.transform.RotationServiceDisabledException;
import org.scohen.juploadr.uploadapi.flickrrest.transform.TemporaryFailureException;

public class SetLocation extends FlickrMethod implements PostUploadAction {
    private String photoId;
    private GeoLocation location;
    
    public SetLocation(UploadImage img) {
        this.photoId = img.getPhotoId();
        this.location = img.getLocation();
    }
    
    public String getErrorText() {
        return "setting location";
    }

    @Override
    protected ProtocolException buildProtocolException(int errorCode) {
        switch (errorCode) {
        case 1:
            return new PhotoNotFoundException();
        case 2:
            return new InvalidRotationException();
        case 3:
            return new TemporaryFailureException();
        case 4:
            return new RotationServiceDisabledException();
        case 5:
             return new NotAvalidAccuracyException();
        case 7: 
              return new UserNotConfiguredException();
        default:
            return null;
        }
    }

    @Override
    public SortedMap<String, String> getParams() {
        SortedMap<String, String> params = new TreeMap<String, String>();
        params.put("api_key", RestFlickrApi.FLICKR_API_KEY);
        params.put("method", "flickr.photos.geo.setLocation");
        params.put("photo_id", photoId);
        params.put("lat", String.valueOf(location.getLatitude()));
        params.put("lon", String.valueOf(location.getLongitude()));
        params.put("accuracy", String.valueOf(location.getAccuracy()));
        
        
        return params;
    }

    @Override
    public DefaultFlickrHandler getResponseHandler() {
        return new NullFlickrHandler(this);
    }

    @Override
    public boolean isAuthorized() {
        return true;
    }
    
    private class NullFlickrHandler extends DefaultFlickrHandler{

        public NullFlickrHandler(Object parent) {
            super(parent);            
        }
        
    }

}
